//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using Eas.WebCrawler.Interfaces;

namespace Eas.WebCrawler.Model.Core {
    internal class ChangeQueue {
        private Eas.WebCrawler.Common.Threading.SpinWaitLock _Lock = new Eas.WebCrawler.Common.Threading.SpinWaitLock();
        private Queue<WorkerState> _Queue = new Queue<WorkerState>();

        public void EnQueueRequest(WorkerState newState) {
            if (newState == WorkerState.Unstarted) throw new ArgumentOutOfRangeException("newState", "Can not change to unstarted state.");

            _Lock.Enter();
            try {
                _Queue.Enqueue(newState);
            }
            finally {
                _Lock.Exit();
            }
        }

        public WorkerState? DeQueueRequest() {
            _Lock.Enter();
            try {
                if (_Queue.Count == 0)
                    return null;
                return _Queue.Dequeue();
            }
            finally {
                _Lock.Exit();
            }
        }
    }
}
